package com.twu.biblioteca;
import com.javafx.tools.doclets.formats.html.SourceToHTMLConverter;

import java.util.ArrayList;
import java.util.Scanner;

public class BibliotecaApp {
    Librarian librarian;
    User user;

    static final String WELCOME = "Welcome to the Bangalore Public Library, good luck finding your book, and remember 'Choose an author as you choose a friend'";
    static final String OPTIONS_INSTRUCTIONS = "Please choose from the following options:";
    static final String LIST_ITEMS = "1. List Available Items";
    static final String LOGIN = "2. Login";
    static final String LOGOUT = "3. Logout";
    static final String QUIT = "9. Quit Bangalore Public Library";
    static final String INVALID = "Invalid entry please try again.";
    static final String TITLE = "Please enter the title of the item you wish to checkout or return:";
    static final String THANK_YOU = "Thanks for visiting, come back soon!";



    public BibliotecaApp(Librarian librarianName) {
        librarian = librarianName;
    }


    public void titleMenu() throws Exception {

        System.out.println(WELCOME);
        System.out.println(OPTIONS_INSTRUCTIONS);
        System.out.println(LIST_ITEMS);
        System.out.println(LOGIN);
        System.out.println(LOGOUT);
        System.out.println(QUIT);
        Scanner reader = new Scanner(System.in);
        Integer selection = 0;
        selection = reader.nextInt();

        switch (selection) {
            case 1:
                for(Book book: librarian.library.availableBooks()) {
                        System.out.println(book.title + " (Book)");
                }
                for(Movie movie: librarian.library.availableMovies()) {
                    System.out.println(movie.title + " (Movie)");
                }
                this.itemOptions();
                break;
            case 2:
                this.loginOptions();
                break;
            case 3:
                this.logOut();
            case 9: System.out.println(THANK_YOU); System.exit(0);
            default: System.out.println(INVALID); break;
        }
    }


    public void itemOptions() throws Exception {
        System.out.println(OPTIONS_INSTRUCTIONS);
        System.out.println("1. Checkout Item");
        System.out.println("2. Return Item");
        System.out.println(QUIT);
        Scanner reader = new Scanner(System.in);
        Integer selection = 0;
        selection = reader.nextInt();

        switch (selection) {
            case 1: try { this.checkoutOptions(); } catch (NullPointerException e) { this.loginOptions();} break;
            case 2: try { this.returnOptions(); } catch (NullPointerException e) { this.loginOptions();} break;
            case 9: System.out.println(THANK_YOU); System.exit(0);
            default: System.out.println(INVALID);
        }
    }

    public void checkoutOptions() throws Exception {
        System.out.println(TITLE);
        Scanner reader = new Scanner(System.in);
        String selection = reader.next();
        System.out.println(user.checkout(selection));
    }

    public void returnOptions() throws Exception {
        System.out.println(TITLE);
        Scanner reader = new Scanner(System.in);
        String selection = reader.next();
        System.out.println(user.returnItem(selection));
    }

    public void loginOptions() throws Exception {
        System.out.println("Please type your membership number");
        Scanner reader = new Scanner(System.in);
        Integer membershipNo = 0;
        membershipNo = reader.nextInt();
        System.out.println("Please type your password");
        String password = reader.next();
        user = new User(" ", " ", password, membershipNo, librarian);
        try { librarian.login(user); } catch (Exception e) { System.out.println(e.getMessage()); this.loginOptions();}
    }

    public void logOut() {
        user.loggedIn = false;

    }

    public static void main(String[] args) throws Exception {
        ArrayList<Book> books = new ArrayList<Book>();
        ArrayList<Movie> movies = new ArrayList<Movie>();
        movies.add(new Movie("ET"));
        movies.add(new Movie("Dumbo"));
        books.add(new Book("Atonement"));
        books.add(new Book("Emma"));
        Library bangalore = new Library(books, movies);
        Librarian lara = new Librarian(bangalore);
        User paprika = new User("Paprika", "paprika@thyme.com", "Hello123", 1234567, lara);
        User cloves = new User("Cloves", "clove@oranges.com", "Hello1234", 1234561, lara);
        lara.users.add(paprika);
        lara.users.add(cloves);
        BibliotecaApp bangalorePublicLibrary = new BibliotecaApp(lara);

        while (true) {
            bangalorePublicLibrary.titleMenu();
        }

    }


}
